﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manyor.Net;

namespace Manyor.Device
{
    public partial class DeviceForm : Form
    {
        SocketServer socketServer;
        CommandDispatcher dispatcher;
        ServerFileTransfer sft;
        public DeviceForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            socketServer = new SocketServer(new Action<byte[], int>(ReceiverMessage));
            sft = new ServerFileTransfer(new Action<byte[]>(SendMessage));
            dispatcher = new CommandDispatcher();
            dispatcher.RegisterCommand(sft,CommandCode.COMMAND_TRANSFER);
            if (socketServer.StartServer())
            {
                this.Text += "------[已启动]";
            }
        }

        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="length"></param>
        public void ReceiverMessage(byte[] bytes, int length)
        {
            dispatcher.DoTask(bytes, length);
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="bytes"></param>
        public void SendMessage(byte[] bytes)
        {
            socketServer.SendMessage(bytes);
        }
    }
}
